As a database engineer, you can use date and
time functions to extract time and date
values from a database. You can perform similar
tasks using the functions available in Python's,
native datetime library. In this video,
you'll learn about the different datetime
functions available in Python and how you
can make use of them. Little Lemon has received several bookings from
guests for tonight. However, the restaurant
has encountered a scheduling conflict
so they need to push each booking slot
forward by one-hour. Python's datetime functions are a great way to
solve this problem. Find out how datetime
functions work, then help Little Lemon to
update their booking slots. Let's begin with an
overview of datetime. Datetime is a Python class with several built-in functions that can be used to format and change time and date variables. It's native to Python, so you can import it
without requiring PIP. Let's review the functions that Python's datetime
library offers. The datetime now function is used to retrieve
today's date. You can also use
datetime.date to retrieve just the date or datetime.time
to call the current time, and the timedelta function calculates the difference
between two values. Now, let's look at the syntax
for implementing datetime. To import the datetime
Python class, use the import code followed
by the library name, then use the as keyword
to create an alias of dt. You can now use this alias
to call the library instead of typing datetime every time
you need to use a function. You now have a datetime object created within your
Python environment. Let's find out how
to make use of its functionality using
the datetime.now function. Begin by creating a variable
called current_time. Next, type the dt alias
as the module name, then use it to call the
datetime.now function. Finally, instruct Python to print the current
date and time values. Execute the code to print the time and date
of your location. Python returns the date
and then the time. The date is displayed in
year, month, day format. The time is displayed in hours,
minutes, seconds format. But what if you
just need to know the current time or maybe
you just want today's date, you can use the same code again, but this time, give Python two separate print instructions. The first instruction
tells Python to print the current date, and the second instruction tells Python to print
the current time. When the code is executed, Python displays each
value separately. Let's look at a slightly more complex
function, timedelta. When making plans, it can be useful to project
into the future, for example, what date is
this same day next week? You can answer questions
like this using the timedelta
function to calculate the difference between
two values and return the result in a
Python friendly format. To find the date in
seven days time, you can create a new
variable called week. Type the dt module and access
the timedelta function as an object instance, then pass through seven
days as an argument. Finally, instruct Python to print the results
of the variable. When executed, Python returns the value of
the date one week from now. Now that you know
how datetime works, let's see if you can
help Little Lemon. As you learned earlier, Little Lemon have encountered
a scheduling conflict. To resolve it, they need to push each booking slot
forward by one hour. You can carry out this task by instructing Python to retrieve the data from the bookings table and then adding one
hour to each booking. Begin by creating a
connection between the front end Python client
and the back-end database, then pass through
your login details. This creates a new
cursor instance and points the cursor at
the Little Lemon database. Before you can begin
working with datetime, you first need to import
the datetime library, use the import keyword and import the library
using the alias dt. This alias is used for
greater efficiency. Next, write a SQL
select statement to return all data from
the bookings table. Pass the statement to the execute module from the
cursor as a string argument. Before entering the loop, instruct Python to print
the column names from the bookings table so that you can view each
item in the row. You can assign these values
to variables and create a new variable
called new booking slot that holds the values
for the updated time slots. To add an hour to
each time slot, pass an argument of one hour to the timedelta function and then add the function to
the booking slot variable. Finally, instruct Python
to print the values for the new booking slots in
the form of a text string. This text string
details the values of each booking ID along with its respective old and
new booking slots. Loop through the results
from the query and extract the rows from the booking ID and
booking slot columns. As the results show, booking ID is the first value, and booking slot is
the fourth value. You should now be familiar with the different datetime functions available in Python and how
you can make use of them. Working with datetime
functions can be difficult, but you've made a great start towards mastering this topic.